package ui.fragment;

import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import net.lucode.hackware.magicindicator.MagicIndicator;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import adapter.fragment.FragmentPagerItem;
import adapter.fragment.FragmentPagerItemAdapter;
import adapter.fragment.FragmentPagerItems;
import base.BaseBean;
import base.fragment.BaseNetFragment;
import bean.UnreadMsgBean;
import event.IntersionEvent;
import event.IntersionNumber;
import student.andlei.com.student.R;
import ui.activity.MessageActivity;
import ui.fragment.intersionfragment.IntersionAddFragment;
import ui.fragment.intersionfragment.IntersionAnswerFragment;
import ui.fragment.intersionfragment.IntersionAskedFragment;
import ui.fragment.intersionfragment.IntersionDetailFragment;
import utils.GsonUtils;
import utils.ViewPagerHelper;
import widget.SViewPager;

/**
 * 问答页
 */
public class IntersionFragment extends BaseNetFragment {

    public static final String GET_UNANSWERED_NUMBER_URL = "/Qa/getUnansweredNumber";

    private MagicIndicator mMagicIndicator;
    private ViewPager mVpLeft;
    //右边界面用ViewPager做缓存处理
    private SViewPager mSvpRight;

    private TextView mTvNumber;
    private TextView tv_msg_number;
    private ImageView img_msg;
    public static Fragment getInstance() {
        return new IntersionFragment();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_intersion;
    }

    @Override
    protected void findViews() {
        img_msg = getView(R.id.img_msg);
        mMagicIndicator = getView(R.id.magic_indicator);
        mVpLeft = getView(R.id.vp_left_content);
        mSvpRight = getView(R.id.vp_right_content);
        mTvNumber = getView(R.id.tv_number);
        tv_msg_number = getView(R.id.tv_msg_number);
    }

    @Override
    protected void formatViews() {
        img_msg.setOnClickListener(this);
        //左边界面
        final FragmentPagerItems leftPages = new FragmentPagerItems(getContext());
        leftPages.add(FragmentPagerItem.of(getResources().getString(R.string.answer_no), IntersionAnswerFragment.getInstance(IntersionAnswerFragment.STATUS_ANSWER_NO)));
        leftPages.add(FragmentPagerItem.of(getResources().getString(R.string.answer_yes), IntersionAnswerFragment.getInstance(IntersionAnswerFragment.STATUS_ANSWER_YES)));
        FragmentPagerItemAdapter leftAdapter = new FragmentPagerItemAdapter(
                getChildFragmentManager(), leftPages);
        mVpLeft.setAdapter(leftAdapter);
        ViewPagerHelper.configIndicatorFixed(mMagicIndicator, mVpLeft, leftPages);
        mVpLeft.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if(position==0){
                    ((IntersionAnswerFragment)leftPages.get(0).instantiate(context,0)).refresh();
                }else {
                    ((IntersionAnswerFragment)leftPages.get(1).instantiate(context,1)).refresh();
                }

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        //左边界面
        FragmentPagerItems rightPages = new FragmentPagerItems(getContext());
        rightPages.add(FragmentPagerItem.of("", IntersionDetailFragment.getInstance()));
        rightPages.add(FragmentPagerItem.of("", IntersionAddFragment.getInstance()));
        rightPages.add(FragmentPagerItem.of("", IntersionAskedFragment.getInstance()));
        FragmentPagerItemAdapter rightAdapter = new FragmentPagerItemAdapter(
                getChildFragmentManager(), rightPages);
        mSvpRight.setAdapter(rightAdapter);
        mSvpRight.setOffscreenPageLimit(3);

    }

    @Override
    protected void formatData() {
        EventBus.getDefault().register(this);
        post(GET_UNANSWERED_NUMBER_URL, BaseBean.class, false);

    }

    @Override
    protected void getBundle() {

    }




    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.img_msg:
                jumpTo(MessageActivity.class);
                break;
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(IntersionEvent event) {
        if (event.isRightPage) {
            mSvpRight.setCurrentItem(event.rightPager);
        } else if (event.isDetailQa) {
            mSvpRight.setCurrentItem(0);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(IntersionNumber event) {
        post(GET_UNANSWERED_NUMBER_URL, BaseBean.class, false);
    }

    @Override
    public void success(String action, BaseBean baseBean) {
        if (TextUtils.equals(action, GET_UNANSWERED_NUMBER_URL)) {
            IntersionNumber intersionNumber = (IntersionNumber) GsonUtils.JSONToObject(GsonUtils.beanToJSONString(baseBean.data), IntersionNumber.class);
            if (intersionNumber.count != 0) {
                mTvNumber.setText(String.valueOf(intersionNumber.count));
                mTvNumber.setVisibility(View.VISIBLE);
            } else {
                mTvNumber.setVisibility(View.GONE);
            }
        } else if (action.equals("personal/getUnreadMsg")) {
            UnreadMsgBean bean = (UnreadMsgBean) GsonUtils.getInstance(context).parseComplexJArrayByCommon(baseBean, UnreadMsgBean.class);
            if (bean.count <= 0) {
                tv_msg_number.setVisibility(View.GONE);
            } else {
                tv_msg_number.setText(bean.count + "");
            }
        }
    }

    @Override
    public void error(String action, Throwable e) {

    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if(!hidden){
           // MainActivity.img_red_point.setVisibility(View.GONE);
            post("personal/getUnreadMsg",BaseBean.class,false);
        }
    }
}
